(function($) {
	$.fn.Tree = function(options_or_method, index) {
		if (!$(this).length) {
			return this;
		}

		if ($.isPlainObject(options_or_method)) {
			// 如果是个初始化
			return this.each(function() {
				var id = $(this).attr('id');
				init(id, options_or_method);
			});
		} else {
			// 如果是一个方法
			var id = $(this).attr('id');
			return do_method(id, options_or_method, index);
		}
	}
	$.fn.Tree.data = [];
	function init(id, options) {
		var _this = this;
		// 默认设置
		var default_settings = {
			key : 'id',
			value : 'menu_text',
			def_data : {},
			rows : {},
			on_change : function() {
			},
			node_style : function() {
			},
		}
		$.fn.Tree.data[id] = $.extend(default_settings, options);
		load_data(id);
	}

	// 方法
	function do_method(id, method, index) {
		switch (method) {
		case 'getselect':
			var opt = $.fn.Tree.data[id];
			return opt.values;
		case 'setvalues':
			set_default_values(id, index);
			return;
		}
	}

	// 加载表内容
	function load_data(id) {
		var opt = $.fn.Tree.data[id];
		var data = $.fn.Tree.data[id];
		var tableobj = $("#" + id);
		tableobj.empty();
		var html = new Array();
		make_table_html(html, id, opt.rows);
		tableobj.append(html.join(''));
		opt.values = opt.def_data;
		// var values = opt.def_data;
		// 给菜单添加点击事件
		tableobj.find(".permissionstree-text").on('click', function(event, selected, isRecursive) {
			// alert(selected);
			var li = $(this).parent();
			var checked = false;
			if (typeof (selected) == "undefined") {
				checked = !li.data("checked");
			} else {
				checked = selected;
			}
			li.data("checked", checked);
			var _key = li.data("key");
			if (checked) {
				$(this).find("i").removeClass("fa-square-o");
				$(this).find("i").removeClass("fa-check-square");
				$(this).find("i").addClass("fa-check-square-o");
				li.find(".permissionstree-op").removeClass("permissionstree-disabled");
				if (!isRecursive) {
					opt.values[_key] = 0;
				}
				li.data("selectCount", li.parent().children().size());
			} else {
				$(this).find("i").removeClass("fa-check-square");
				$(this).find("i").removeClass("fa-check-square-o");
				$(this).find("i").addClass("fa-square-o");
				li.find(".permissionstree-op").addClass("permissionstree-disabled");
				li.find(".permissionstree-op i").removeClass("fa-check-square-o");
				li.find(".permissionstree-op i").addClass("fa-square-o");
				li.data("selectCount", 0);
				delete opt.values[_key];
			}
			if (typeof (selected) == "undefined" && li.parent().parent().is("li")) {
				var parentli = li.parent().parent();
				var selectCount = parentli.data("selectCount");
				if (!selectCount) {
					selectCount = 0;
				}
				if (checked) {
					selectCount++;
				} else {
					selectCount--;
				}
				if (selectCount == 0) {
					parentli.children(":first").find("i").removeClass("fa-check-square-o");
					parentli.children(":first").find("i").removeClass("fa-check-square");
					parentli.children(":first").find("i").addClass("fa-square-o");
					parentli.data("checked", false);
					delete opt.values[parentli.data("key")];
				} else if (selectCount == li.parent().children().size()) {
					parentli.children(":first").find("i").removeClass("fa-square-o");
					parentli.children(":first").find("i").removeClass("fa-check-square");
					parentli.children(":first").find("i").addClass("fa-check-square-o");
					parentli.data("checked", true);
					opt.values[parentli.data("key")] = 0;
				} else {
					parentli.children(":first").find("i").removeClass("fa-square-o");
					parentli.children(":first").find("i").removeClass("fa-check-square-o");
					parentli.children(":first").find("i").addClass("fa-check-square");
					parentli.data("checked", true);
					opt.values[parentli.data("key")] = 0;
				}
				parentli.data("selectCount", selectCount);
			}
			if (!isRecursive) {
				// tableobj.data("values", values);
				opt.on_change(opt.values);
				li.find("ul .permissionstree-text").trigger("click", [ checked ]);
			}
		});
		// 给操作添加点击事件
		tableobj.find(".permissionstree-op").on('click', function(event, isRecursive) {
			if (!$(this).is(".permissionstree-disabled")) {
				var op = parseInt($(this).data("key"));
				var per = $(this).parent().data("key");
				if ($(this).find("i").is(".fa-square-o")) {
					$(this).find("i").removeClass("fa-square-o");
					$(this).find("i").addClass("fa-check-square-o");
					if (!isRecursive) {
						opt.values[per] += op;
					}
				} else {
					$(this).find("i").removeClass("fa-check-square-o");
					$(this).find("i").addClass("fa-square-o");
					if (!isRecursive) {
						opt.values[per] -= op;
					}
				}
				if (!isRecursive) {
					opt.on_change(opt.values);
				}
			}
		});
		set_default_values(id, opt.values);
	}
	function set_default_values(id, values) {
		var tableobj = $("#" + id);
		tableobj.find(".permissionstree-li").trigger("click", [ false, true ]);
		for ( var _k in values) {
			tableobj.find(".permissionstree-li[data-key='" + _k + "']>:first").trigger("click", [ true, true ]);
			var op = caseOperation(values[_k]);
			for (var i = 0; i < op.length; i++) {
				tableobj.find(".permissionstree-li[data-key='" + _k + "']>.permissionstree-op[data-key='" + op[i] + "']").trigger("click",
						[ true ]);
			}
		}
		$.fn.Tree.data[id].values = values;
	}
	// 分割操作权限
	var oper = [ 32, 16, 8, 4, 2, 1 ];
	function caseOperation(op) {
		var arr = [];
		for (var i = 0; i < oper.length; i++) {
			if (op & oper[i]) {
				arr.push(oper[i]);
			}
		}
		return arr;
	}
	// 制作 表数据
	function make_table_html(html, id, rows) {
		var opt = $.fn.Tree.data[id];
		html.push('<ul class="permissionstree-ul">');
		for (var i = 0; i < rows.length; i++) {
			html.push('<li class="permissionstree-li" data-checked="false" data-key="');
			html.push(rows[i][opt["key"]]);
			html.push('"><div class="permissionstree-text"><i class="fa fa-square-o"></i>');
			html.push(rows[i][opt["value"]]);
			html.push('</div>');
			// alert(rows[i]["operation"]);
			var operation = $.parseJSON(rows[i]["operation"]);
			for ( var op in operation) {
				html.push('<div class="permissionstree-op permissionstree-disabled" data-key="');
				html.push(operation[op]);
				html.push('"><i class="fa fa-square-o"></i>');
				html.push(op);
				html.push('</div>');
			}
			html.push('<div class="permissionstree-clear"></div>');
			if (rows[i].son) {
				make_table_html(html, id, rows[i].son);
			}
			html.push('</li>');
		}
		html.push('</ul>');
	}

})(jQuery);